// spawner.txt

// This creature is largely inert. It just sites there. But, every few turns, it 
// spits out a number of hostile creatures to bedevil the party.

// The creature will only spit out so many groups of creatures. The number is 
// kept track of in a Stuff Done Flag you provide. If you don't provide one, 
// the creature spits out unlimited foes. Foes created by a spawner do not
// provide experience or treasure.

// Memory Cells:
//   Cell 0 - The creature type it spits out.
//   Cell 1 - How many of it it spits out. Defaults to 1 if left at 0.
//   Cell 2 - How many turns elapse between each spawn. Defaults to 4 if left at 
//     0.
//   Cells 3,4 - The coordinates of a Stuff Done Flag. This flag is 
//     incremented by one whenever the spawner makes creatures. If both 
//     of these are left at 0, the spawner makes unlimited creatures.
//   Cell 5 - The maximum number of groups of creatures this spawner makes. If
//     left at 0, makes an unlimited amount.

begincreaturescript;

variables;

short i,target;
short last_abil_time;
short turns_between = 4;
short num_to_make = 1;
short can_spawn = 1;

body;

beginstate INIT_STATE;
	last_abil_time = get_current_tick();
	
	if (get_memory_cell(1) > 0)
		num_to_make = get_memory_cell(1);
	if (get_memory_cell(2) > 0)
		turns_between = get_memory_cell(2);
	break;

beginstate DEAD_STATE;
 if (get_flag(4,15) == 0){
set_flag(4,15,1);
 }
	break;

beginstate START_STATE; 
	if (((get_memory_cell(3) > 0) || (get_memory_cell(4) > 0)) && (get_memory_cell(5) > 0)) {
		if (get_flag(get_memory_cell(3),get_memory_cell(4)) >= get_memory_cell(5))
			can_spawn = 0;
		}
		
	// Look for a target, maybe spawn critters
	if (select_target(ME,8,0)) {
		if ((dist_to_party() <= 8) && (get_attitude(ME) >= 10) && (tick_difference(last_abil_time,get_current_tick()) >= turns_between) && (can_spawn > 0)) {
			print_named_str(ME,"spawns another Darkling!");
			play_sound(-168);
			i = 0;
			while (i < num_to_make) {
				place_monster(get_memory_cell(7),get_memory_cell(8),get_memory_cell(0),1);
				i = i + 1;
				}
			last_abil_time = get_current_tick();
			if ((get_memory_cell(3) > 0) || (get_memory_cell(4) > 0)) 
				inc_flag(get_memory_cell(3),get_memory_cell(4),1);
			}
		}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;
 